<?php

class EntityController extends Controller
{
    public function actionCreate(){
        /*
        $entity = array(
            "type"=>"1",//news
            "url"=>"http://baidu.com",
            "title"=>"baidu",
            "content"=>"content,haha",
            );
        
        $entity = array(
            "type"=>"2",//location
            "longitude"=>"120.11",
            "latitude"=>"-40.00",
            "height"=>"1212.11",
            );
        */
        if(!empty($_POST)){
            if($_POST['type'] == "1"){
                $html = array(
                    "title"=>$_POST['title'],
                    "content"=>$_POST['content'],
                    "url"=>$_POST['url'],
                    );
                $modelEntityHtml = new EntityHtml;
                $modelEntityHtml->attributes = $html;
                if($modelEntityHtml->save()){
                    $real_id = $modelEntityHtml->attributes->id;
                }
            }else if($_POST['type'] == "2"){
                $location = array(
                    "height"=>$_POST['height'],
                    "longitude"=>$_POST['longitude'],
                    "latitude"=>$_POST['latitude'],
                    );
                $modelEntityLocation = new EntityLocation;
                $modelEntityLocation->attributes = $location;
                if($modelEntityLocation->save()){
                    $real_id = $modelEntityLocation->attributes->id;
                }
            }
            $model = new Entity;
            $model->attributes->id = $real_id;
            $model->attributes->name = $_POST['name'];
            $model->attributes->type = $_POST['type'];
            if($model->save()){
                $this->responseJSON($model->attributes);
            }
        }else{
            $this->responseJSON("parmas empty");
        }
    }

    public function actionRead($id){

    }

    public function actionUpdate($id){

    }

    public function actionDelete($id){
        $model = $this->loadModel($id);
        if($model->delete()){
            $this->responseJSON("success");
        }
    }

    public function loadModel($id){
        $model=Entity::model()->findByPk($id);
        if($model===null)
            $this->responseJSON("404");
        return $model;
    }
    
}
